home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_3.lha / 8_3 / 8_3_file.c < prev    next >
Text File  |  1993-08-08  |  909b  |  43 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Exercise 8.3
  6. / Read in a file name, with error checking
  7. include <stream.h>
  8. include <stdio.h>
  9. ifndef FILENAME_MAX    /* DELETE */
  10. define FILENAME_MAX 1024    /* DELETE */
  11. endif    /* DELETE */
  12.  
  13. nt read_filename(ostream &out, istream &in, char **val)
  14.  
  15.    // set up flushing of the output stream
  16.    ostream *old = in.tie(&out);
  17.  
  18.    // loop until we get something right
  19.    for ( ; ; out << "Try again\n")
  20. {
  21. out << "Type a file name: ";
  22.  
  23. // read a line, including the newline
  24. char buf[FILENAME_MAX], c;
  25. if (!cin.get(buf, FILENAME_MAX))
  26.     {
  27.     in.tie(old);
  28.     return 0;
  29.     }
  30. in.get(c);
  31.  
  32. // check the value
  33. if (!validfilename(buf))
  34.     continue;
  35.  
  36. // return the value, restoring the old tie first
  37. *val = new char[strlen(buf) + 1];
  38. strcpy(*val, buf);
  39. in.tie(old);
  40. return 1;
  41. }
  42.  
  43.